package com.fraud;

import static org.junit.Assert.*;

import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderErrors;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.logger.KnowledgeRuntimeLogger;
import org.drools.logger.KnowledgeRuntimeLoggerFactory;
import org.drools.runtime.StatefulKnowledgeSession;
import org.junit.Before;
import org.junit.Test;

public class TestFraudDetection{

    KnowledgeBase kbase;
    StatefulKnowledgeSession ksession;


	@Before
	public void setUp() throws Exception {
        this.kbase = readKnowledgeBase();
        this.ksession = kbase.newStatefulKnowledgeSession();
		this.ksession.insert(new CommonCountries());
	}
	
	public void tearDown()
	{
		this.ksession.dispose();
	}
	
    private static KnowledgeBase readKnowledgeBase() throws Exception {
	        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
	        kbuilder.add(ResourceFactory.newClassPathResource("Fraud.drl"), ResourceType.DRL);
	        KnowledgeBuilderErrors errors = kbuilder.getErrors();
	        if (errors.size() > 0) {
	            for (KnowledgeBuilderError error: errors) {
	                System.err.println(error);
	            }
	            throw new IllegalArgumentException("Could not parse knowledge.");
	        }
	        KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
	        kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
	        return kbase;
	    }

	    
	    
	@Test
	public void testOkCustomerBigOrder() {
		Customer customer=new Customer("Bob", 0, "Finland", "Finland", 2000, Customer.CREDIT_CARD);
		this.ksession.insert(customer);
		ksession.fireAllRules();
		assertEquals("Failed Customer should have been ok",false, customer.isRequiresCheck());

	}


	@Test
	public void testCheckCustomerDifferentCountry() {
		Customer customer=new Customer("Bob", 0, "Finland", "Germany", 2000, Customer.CREDIT_CARD);
		this.ksession.insert(customer);
		ksession.fireAllRules();
		assertEquals("Failed Customer should have required check",true,customer.isRequiresCheck());
		
	}

	@Test
	public void testCheckCustomerDifferentCountrySmallOrder() {
		Customer customer=new Customer("Bob", 0, "Finland", "Germany", 200, Customer.CREDIT_CARD);
		this.ksession.insert(customer);
		ksession.fireAllRules();
		assertEquals("Failed Customer should have been ok",false,customer.isRequiresCheck());
		
	}

	@Test
	public void testCheckCustomeUnusualCountry() {
		Customer customer=new Customer("Bob", 0, "Finland", "Uganda", 200, Customer.CREDIT_CARD);
		this.ksession.insert(customer);
		ksession.fireAllRules();
		assertEquals("Failed Customer should have required check",true,customer.isRequiresCheck());
		
	}
	
	
	@Test
	public void testCheckBigOrder() {
		Customer customer=new Customer("Bob", 0, "Finland", "Finland", 20005, Customer.CREDIT_CARD);
		this.ksession.insert(customer);
		ksession.fireAllRules();
		assertEquals("Failed Customer should have required check",true,customer.isRequiresCheck());
		
	}

	@Test
	public void testCheckBigOrderDifferentCountry() {
		Customer customer=new Customer("Bob", 0, "Finland", "Germany", 20005, Customer.CREDIT_CARD);
		this.ksession.insert(customer);
		ksession.fireAllRules();
		assertEquals("Failed Customer should have required check",true,customer.isRequiresCheck());
		
	}	
	
	
	@Test
	public void testCheckUsingBank() {
		Customer customer=new Customer("Bob", 0, "Finland", "Germany", 2000, Customer.BANK);
		this.ksession.insert(customer);
		ksession.fireAllRules();
		assertEquals("Failed Customer should have been ok",false,customer.isRequiresCheck());

	}

	
}
